package com.zt.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.reggie.common.CustomException;
import com.zt.reggie.dto.SetMealDto;
import com.zt.reggie.entity.SetMeal;
import com.zt.reggie.entity.SetMealDish;
import com.zt.reggie.mapper.SetMealMapper;
import com.zt.reggie.service.SetMealDishService;
import com.zt.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zt.reggie.service.impl
 * @since 2023 - 02 - 25 - 15:24
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {
    @Autowired
    private SetMealDishService setMealDishService;
    @Override
    @Transactional
    public void saveWithDish(SetMealDto setMealDto) {
        //保存套餐基本信息
        this.save(setMealDto);

        //保存套餐菜品信息
        List<SetMealDish> setMealDishList = setMealDto.getSetmealDishes();

        setMealDishList = setMealDishList.stream().map(e ->{
            //设置套餐id
            e.setSetmealId(setMealDto.getId());
            return e;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setMealDishList);
    }

    @Override
    @Transactional
    public void deleteWithDish(List<Long> ids) {
        //判断套餐状态 set_meal
        LambdaQueryWrapper<SetMeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetMeal::getId, ids);
        wrapper.eq(SetMeal::getStatus, 1);
        int count = this.count(wrapper);
        if(count > 0){
            throw new CustomException("套餐正在售卖，不能删除!");
        }

        //删除套餐
        this.removeByIds(ids);

        //删除套餐和菜品的关系 set_meal_dish
        LambdaQueryWrapper<SetMealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetMealDish::getSetmealId, ids);
        setMealDishService.remove(wrapper1);
    }

    @Override
    public SetMealDto getByIdWithDish(Long id) {
        SetMealDto setMealDto = new SetMealDto();
        //套餐基本信息
        SetMeal setMeal = this.getById(id);
        BeanUtils.copyProperties(setMeal, setMealDto);

        //菜品信息
        LambdaQueryWrapper<SetMealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMealDish::getSetmealId, id);
        wrapper.orderByAsc(SetMealDish::getSort);

        List<SetMealDish> list = setMealDishService.list(wrapper);
        setMealDto.setSetmealDishes(list);

        return setMealDto;
    }

    @Override
    public void updateWithDish(SetMealDto setMealDto) {
        //修改基本信息
        this.updateById(setMealDto);

        //修改菜品信息
        //1.先删除
        LambdaQueryWrapper<SetMealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetMealDish::getSetmealId, setMealDto.getId());
        setMealDishService.remove(wrapper);
        //2.再保存
        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();
        setMealDishes = setMealDishes.stream().map(e -> {
            //设置套餐id
            e.setSetmealId(setMealDto.getId());
            return e;
        }).collect(Collectors.toList());
        setMealDishService.saveBatch(setMealDishes);
    }

    @Override
    public void changeStatus(List<Long> ids, Integer status) {
        LambdaUpdateWrapper<SetMeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SetMeal::getId, ids);
        wrapper.set(SetMeal::getStatus, status);

        this.update(wrapper);
    }
}
